Thread: VERY simple problem, can't figure it out for the life of me. :[

  1. #16
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by rasheemo View Post
    so can't i just send '&start' to pass the pointer itself? but the compiler won't allow that for some reason.
    Nope. I was confused by this too (but it seems very straightforward to me now).

    & the address of operator is something to use when you want to pass the address of a variable.

    In C, all function arguments are just VALUES. So
    Code:
    int x;
    myfunc(&x);
    now, the prototype for myfunct must be
    Code:
    myfunc(int *ptr);
    So, the VALUE received is a memory address where an int (another value) is stored. If you want to change the value of that int in myfunc, you would use * to dereference the pointer:
    Code:
    *ptr = 666;
    that means to change the value stored at the address x to 666. That is very different from this:
    Code:
    ptr = 666;
    This is probably not a valid memory address, but you can assign it anyway. You did not change the value stored at the address represented by ptr; you changed the address represented by ptr. So now, in myfunc, ptr refers to A DIFFERENT ADDRESS than the one supplied as a parameter. In other words, ptr now has nothing to do with x.

    The same thing is true if you use malloc, because ptr = malloc(whatever) will assign a NEW ADDRESS to the value of ptr. Remember, ptr is not x; its value used to be the address of x, but you just changed that.

    You can dereference a struct pointer:
    Code:
    #include <stdio.h>
    
    struct x{
    	int i;
    };
    
    void test(struct x *ptr) {
    	(*ptr).i = 6;
    }
    
    int main(void) {
    	struct x eg;
    	test(&eg);	
    	printf("%d",eg.i);
    	return 0;
    }
    However, the normal way to do things with a linked list is to use return values:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    struct x{
    	int i;
    };
    
    struct x *test() {
    	struct x *here = malloc(sizeof(struct x));
    	return here;
    }
    
    int main(void) {
    	struct x *eg = test();
    	eg->i = 6;
    	printf("%d",eg->i);
    	return 0;
    }
    Last edited by MK27; 09-27-2009 at 01:46 PM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  2. #17
    Registered User
    Join Date
    Sep 2009
    Posts
    11
    ok, i figured out how to build the linked list. but now my program freezes when i try to copy a string to string inside of the struct.

    here is the latest code:

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include "template_wordstat.h"
    
    entry * append() ;
    
    int main(int argc, char* argv[])
    {
        FILE* fp;
        char line[MAX_LINE];
        int i = 0;
        char *nextWord;
        entry *start = NULL;
    
    
        /* Checks for correct usage and handles arguments accordingly */
    
        if (argc != 2) {
            fprintf(stderr, "Error: wrong number of parameters.\n");
            return -1;
        }
    
    	if (strcmp(argv[1],"-h") == 0) {
            	printf("Usage: wordstat FILE_NAME\n");
            	return 0;
       	}
    
        if ((fp=fopen(argv[1],"r")) == NULL) {
            fprintf(stderr, "Error: cannot open file %s.\n", argv[1]);
            return -2;
        }
    
        while (! feof(fp)) {
    
            /* reads one line at a time */
    
                if (fgets(line, MAX_LINE, fp) == NULL)
                        break;
                if (line[0] == '\n')    /* empty line */
                        continue;
    
            /* extract words from the line and handle the words	*/
    
            nextWord = strtok (line, TS);
    
            /* if the first token is empty, then there are no words */
    
            if(nextWord == NULL){
                fprintf(stderr, "File contains no words.\n");
                return -2;
            }
    
            /* go through each word and build linked list with entries*/
    
            while(nextWord != NULL){
                entry *marker;
                entry *temp = append();
    
                marker = start;
                printf(nextWord); //this prints the word just fine
    
    ////////////////////////////// here is where the trouble starts
    
                temp->word = malloc(sizeof(strlen(nextWord)+1));
                strcpy(temp->word, nextWord);
    
    ////////////////////////////// if i remove the above 2 lines, my program runs and ends fine
    
                temp->count = 0;
    
                /* If the very first node is empty */
                if (start == NULL) {
                    start = temp;
                    start -> next = NULL;
                }
                else {
                    while(marker -> next != NULL)
                        marker = marker -> next;
    
                    temp -> next = NULL;
                    marker -> next = temp;
                }
    
                nextWord = strtok (NULL, TS);
            }
        }
    
    	/*	sorting and printing */
    
    	fclose(fp);
    
    
        	return 0;
    }
    
    entry *append() {
        entry *temp = malloc(sizeof(entry));
        return temp;
     }
    i'm so frustrated :[

  3. #18
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You don't want sizeof in your malloc statement.

  4. #19
    Registered User
    Join Date
    Sep 2009
    Posts
    11
    oh wow. that was it. thank you so much.

  5. #20
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by rasheemo View Post
    Code:
    while(curr){
                    printf("traversing!");
                    curr = curr->next;
                }
    I set curr to the next one linked to it until i get to the last node (NULL).

    Code:
    if ((curr = (struct wordEntry*)malloc(sizeof(entry))) == NULL)
    I allocate space for the curr, which is located at the null entry.
    What's the point of your loop, if you're just overwriting 'cur' anyway? For that matter, what's the point of your loop anyway? All it does is run to the end of the chain and end up with a NULL pointer. You may as well have just done:
    Code:
    cur = NULL;
    ...and saved yourself the trouble.


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  2. Replies: 5
    Last Post: 12-03-2003, 05:47 PM
  3. Im a Newbie with a graphics design problem for my simple game
    By Robert_Ingleby in forum C++ Programming
    Replies: 1
    Last Post: 11-23-2001, 06:41 PM
  4. Simple boolean problem
    By larry in forum C++ Programming
    Replies: 9
    Last Post: 10-13-2001, 08:49 AM
  5. A Simple (?) Problem
    By Unregistered in forum C++ Programming
    Replies: 8
    Last Post: 10-12-2001, 04:28 AM